﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;

[assembly: System.Web.UI.TagPrefix("TD.web.controls", "TD")]
namespace TDLib.Controls
{
    [Designer("TDLib.Controls.PagerControlDesign"), ToolboxData("<{0}:PagerControl runat=server></{0}:PagerControl>")]
    public class PagerControl : System.Web.UI.WebControls.WebControl
    {
        HtmlTextWriter output;
        StringBuilder Rowsb;
        private LinkButton lb;
        private Label lbl;
        private TextBox tb;
        private Button bt;
        private void SetRowsb()
        {
            Rowsb = new StringBuilder(200);

            StringBuilder Formater = new StringBuilder(50);


            if (pagecount > 0)
            {

                for (int j = ((curpage - 5 > 1) ? (curpage - 5) : 1); j <= pagecount && j < (((curpage - 5 > 1) ? (curpage - 5) : 1) + 10); j++)
                {
                    lb = new LinkButton();
                    lb.Text = j.ToString();
                    if (j == curpage)
                    {
                        lb.Enabled = false;
                        Controls.Add(lb);
                        //Rowsb.Append(string.Format("<strong><font color='#FF0000'> {0} </font></strong>", j));
                    }
                    else
                    {
                        Controls.Add(lb);
                        //Rowsb.Append(string.Format("<font color='0044DD'> <a href='ArticleList.aspx?ColumnId={0}&CurPage={1}'>{1}</a> ", columnid, j));
                    }
                }

            }
        }



        protected override void CreateChildControls()
        {
            string mark = "?";
            if (Regex.IsMatch(postbackurl, "/?"))
                mark = "&";
            if (pagecount > 0)
            {
                string spans;
                if (cssclass != string.Empty)
                    spans = string.Format("<span id={0} class={1}>", this.UniqueID, cssclass);
                else
                    spans = string.Format("<span id={0}>", this.UniqueID);
                Controls.Add(new LiteralControl(spans));
                lbl = new Label();
                lbl.Text = string.Format("『共{0}页』,当前第{1}页", pagecount, curpage);
                Controls.Add(lbl);
                //首页
                lb = new LinkButton();
                lb.CausesValidation = false;
                lb.PostBackUrl = string.Format("{0}{1}CurPage={2}", postbackurl, mark, 1);
                lb.Text = "首页";
                if (curpage == 1)
                {
                    lb.Enabled = false;
                    Controls.Add(lb);
                }
                else
                {
                    Controls.Add(lb);
                }
                Controls.Add(new LiteralControl("&nbsp;"));
                //前一页
                lb = new LinkButton();
                lb.CausesValidation = false;
                lb.PostBackUrl = string.Format("{0}{1}CurPage={2}", postbackurl, mark, curpage - 1);
                lb.Text = "上一页";
                if (curpage <= 1)
                {
                    lb.Enabled = false;
                    Controls.Add(lb);
                }
                else
                {
                    Controls.Add(lb);
                }
                Controls.Add(new LiteralControl("&nbsp;"));

                //当前页附近8页
                for (int j = ((curpage - 4 > 1) ? (curpage - 4) : 1); j <= pagecount && j < (((curpage - 4 > 1) ? (curpage - 4) : 1) + 8); j++)
                {
                    lb = new LinkButton();
                    lb.CausesValidation = false;
                    lb.PostBackUrl = string.Format("{0}{1}CurPage={2}", postbackurl, mark, j);
                    lb.Text = string.Format("[{0}]", j);
                    if (j == curpage)
                    {
                        lb.Enabled = false;
                        Controls.Add(lb);
                    }
                    else
                    {
                        Controls.Add(lb);
                    }
                    Controls.Add(new LiteralControl("&nbsp;"));
                }

                //后一页
                lb = new LinkButton();
                lb.CausesValidation = false;
                lb.PostBackUrl = string.Format("{0}{1}CurPage={2}", postbackurl, mark, curpage + 1);
                lb.Text = "下一页";
                if (curpage >= pagecount)
                {
                    lb.Enabled = false;
                    Controls.Add(lb);
                }
                else
                {
                    Controls.Add(lb);
                }
                Controls.Add(new LiteralControl("&nbsp;"));

                //末页
                lb = new LinkButton();
                lb.CausesValidation = false;
                lb.PostBackUrl = string.Format("{0}{1}CurPage={2}", postbackurl, mark, pagecount);
                lb.Text = "尾页";
                if (curpage == pagecount)
                {
                    lb.Enabled = false;
                    Controls.Add(lb);
                }
                else
                {
                    Controls.Add(lb);
                }
                Controls.Add(new LiteralControl("&nbsp;"));

                Controls.Add(new LiteralControl("</span>"));
            }
        }

        #region 属性
        [Category("内容"), Description("页数")]
        public int PageCount
        {
            get { return pagecount; }
            set { pagecount = value; }
        }
        [Category("内容"), Description("当前页码")]
        public int CurPage
        {
            get { return curpage; }
            set { curpage = value; }
        }

        [Category("样式"), Description("样式表")]
        public string CssClass
        {
            get { return cssclass; }
            set { cssclass = value; }
        }

        [Category("内容"), Description("回传地址")]
        public string PostBackUrl
        {
            get { return postbackurl; }
            set { postbackurl = value; }
        }

        [Category("内容"), Description("每页内容数目")]
        public string PageSize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }

        #endregion
        #region 字段

        private int pagecount;
        private int curpage;
        private string postbackurl;
        private string cssclass;
        private string pagesize;
        #endregion
    }
}
